// JavaScript Document

$mangMucGoc = null;
$mangTongDiemMuc = null;

function checkDangNhap(){
	if (document.frmdangnhap.txtmaso.value=="" || document.frmdangnhap.txtmatkhau.value==""){
		alert ("Tên đăng nhập và mật khẩu không được để trống!");
		return false;
	}
	return true;
}

function thoat() {
	if (confirm('Bạn có chắc chắn muốn thoát không?'))
		window.location = 'views/DangXuat.php';
}

function layMangMucGoc() {
	$MUC_GOC = $('td[id^="MUC_"]');
	mangMuc = new Array();
	for (i=0; i < $MUC_GOC.length; i++) {
		temp = $MUC_GOC[i].id.split("_");
		obj = new Object();
		obj.maMuc = parseInt(temp[1]);
		obj.diemToiDa = parseInt(temp[3]);
		mangMuc.push(obj);
	}
	return mangMuc;
}

function capNhatTongDiemCuaMuc(index) {
	tieuChi = $('select[name^="'+$prefix+'Muc'+$mangMucGoc[index].maMuc+'_cboTC"]');
	muc = $('#' + $prefix + 'TongDiemMuc_' + $mangMucGoc[index].maMuc);
	
	diemCu = parseInt(muc.html());
	diemMoi = 0;

	for (k=0; k<tieuChi.length; k++)
		diemMoi += parseInt(tieuChi[k].value);
	
	if (diemMoi > $mangMucGoc[index].diemToiDa)
		diemMoi = $mangMucGoc[index].diemToiDa;
	
	muc.text(diemMoi);
	
	tongDiem = $('#' + $prefix + 'TONG_DIEM');
	tongDiem.text(parseInt(tongDiem.html()) + diemMoi - diemCu);
}

function capNhatTongDiemCacMuc() {
	for (i=0; i<$mangMucGoc.length; i++)
		capNhatTongDiemCuaMuc(i);
}

function chonDiemNgauNhien() {
	$tieuChi = $('select[name^="' + $prefix + 'Muc"]');
	for (i = 0; i < $tieuChi.length; i++) {
		cbo = $tieuChi[i];
		num = Math.floor(Math.random()*cbo.length);
		cbo.selectedIndex = num;
		//console.log(cbo.name + ' = ' + num);
	}
	capNhatTongDiemCacMuc();
}

function saoChepTuDiemSinhVien() {
	diemSV = $('select[name^="SV_Muc"]');
	diemUser = $('select[name^="' + $prefix + 'Muc"]');
	for (i = 0; i < diemSV.length; ++i)
		diemUser[i].selectedIndex = diemSV[i].selectedIndex;
	capNhatTongDiemCacMuc();
}

$(document).ready(function() {
	patternBangDiem = /^\?page=bangdiem/i;
	if (patternBangDiem.test(location.search)) {
		$mangMucGoc = layMangMucGoc();
		
		pre = $prefix;
		$prefix = "SV_";
		capNhatTongDiemCacMuc();
		$prefix = "CVHT_";
		capNhatTongDiemCacMuc();
		$prefix = "HDK_";
		capNhatTongDiemCacMuc();
		$prefix = pre;
	}
});

function chamDiemSinhVien(mssv) {
	window.location = 'http://' + location.hostname + location.pathname + '?page=bangdiem&mssv=' + mssv;
}

function xemDanhSachSinhVien(malop) {
	window.location = 'http://' + location.hostname + location.pathname + '?page=danhsachlop&malop=' + malop;
}